home *** CD-ROM | disk | FTP | other *** search
- /* Offworld CopyBits Example project
- *
- * This code is public domain.
- * written by Ken Worley 7/6/94
- * AOL KNEworley
- *
- * This code creates a small color window and an offscreen graphics world
- * (GWorld) the same size as the window. It then uses the GWorld to smoothly
- * animate a series of color icons with a horizontal line that moves down
- * behind the icon.
- */
-
- #define kWindowID 128
- #define kStartIconID 128
- #define kFinishIconID 145
-
- #include <QDOffscreen.h> /* for GWorld routines */
- /* MacHeaders should get everything else needed */
-
- void InitToolbox(void);
-
- void InitToolbox()
- {
- /* Initialize the Mac Toolbox Managers */
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- FlushEvents(everyEvent,0);
- TEInit();
- InitDialogs(0L);
- InitCursor();
- }
-
- void main( void )
- {
- WindowPtr myWindow;
- PixMapHandle thePixMap;
- GWorldPtr myOffscreen;
- GDHandle winDevice;
- CGrafPtr winPort;
- RGBColor myBlack;
- RGBColor myRed;
- QDErr myQDErr;
- OSErr myOSErr;
- Rect myRect;
-
- /* Initialize the Mac toolbox */
- InitToolbox();
-
- /* Set up my RGB black */
- myBlack.red = myBlack.green = myBlack.blue = 0;
-
- /* Get my color window from the resource, show it, and make it the current port */
- myWindow = GetNewCWindow( kWindowID, NULL, (WindowPtr)(-1L) );
- ShowWindow( myWindow );
- SetPort( myWindow );
- /* Save the window's port and device */
- GetGWorld( &winPort, &winDevice );
-
- /* Create an offscreen graphics world the same size as my window */
- /* Sending 0 as the second argument (the pixel depth) causes NewGWorld() to */
- /* examine the global rectangle sent as the third argument and determine the */
- /* deepest pixel map needed for that rectangle even if it crosses devices. */
-
- myQDErr = NewGWorld( &myOffscreen, 0, &(myWindow->portRect), NULL, NULL, 0L );
-
- /* If successfully created the graphics world, set it up to draw in */
- if ( myQDErr == noErr )
- {
- /* Get the offscreen graphics world's pixel map handle */
- thePixMap = GetGWorldPixMap( myOffscreen );
- /* Lock the pixels so they won't be moved while we're drawing */
- LockPixels( thePixMap );
- /* Make my offscreen graphics world the current port */
- SetGWorld( myOffscreen, NULL );
- /* set the foreground color to black */
- RGBForeColor( &myBlack );
- /* Set the pen size to 1x4 */
- PenSize( 1, 4 );
- }
- else
- myOffscreen = NULL;
-
- /* Draw in the offscreen graphics world and animate icon in window with CopyBits */
-
- if ( myOffscreen ) /* if graphics world is there */
- {
- Rect tempRect;
- short x;
- CIconHandle myICON;
- long ignore;
-
- /* set up an icon-sized rectangle in the center of the window */
- tempRect = myOffscreen->portRect;
- SetRect( &tempRect, ((tempRect.right - tempRect.left)/2)-16,
- ((tempRect.bottom - tempRect.top)/2)-16,
- ((tempRect.right - tempRect.left)/2)+16,
- ((tempRect.bottom - tempRect.top)/2)+16);
-
- /* loop through icons used for animation frames */
- for ( x=kStartIconID; x<=kFinishIconID; x++ )
- {
- /* Erase the whole offscreen rectangle */
- EraseRect( &(myOffscreen->portRect) );
- /* draw a horizontal line that moves down the window */
- MoveTo( myOffscreen->portRect.left,
- tempRect.top + x - kStartIconID );
- LineTo( myOffscreen->portRect.right,
- tempRect.top + x - kStartIconID );
- /* load the icon */
- myICON = GetCIcon( x );
- /* draw icon in offscreen graphics world */
- /* draw the icon */
- PlotCIcon( &tempRect, myICON );
- /* copy offscreen world to window */
- /* make window current port */
- SetGWorld( winPort, winDevice );
- /* set foreground color to black (affects CopyBits) */
- RGBForeColor( &myBlack );
- /* copy the image */
- CopyBits(
- &(((GrafPtr)myOffscreen)->portBits),//bitmap of gworld
- &(((GrafPtr)myWindow)->portBits), //bitmap of window
- &(myOffscreen->portRect), //portRect of gworld
- &(myWindow->portRect), //portRect of window
- srcCopy, NULL );
- /* make offscreen world current port again */
- SetGWorld( myOffscreen, NULL );
- /* dispose of the icon */
- DisposeCIcon( myICON );
- /* Delay to slow animation (1/10 second) */
- Delay( 6, &ignore );
- } /* for */
- } /* if */
-
- /* Unlock the offscreen world's pixel map */
- if ( myOffscreen ) UnlockPixels( thePixMap );
- /* Dispose of the offscreen graphics world */
- if ( myOffscreen ) DisposeGWorld( myOffscreen );
- /* make the window the current port */
- SetGWorld( winPort, winDevice );
- /* Write message in window */
- MoveTo( myWindow->portRect.left + 5, myWindow->portRect.bottom - 14 );
- DrawString( "\pclick to exit" );
- /* Wait for a mouseDown to quit */
- while ( !Button() ) {}
- }
-